import React, { useState, useMemo } from "react";
import { View } from "@tarojs/components";
import Taro from "@tarojs/taro";
import { fakeSendSmsCode } from '@/server/login'
import { toast, goBack } from "@/utils";
const smsBtn = ({ mobile = "", dispatch }) => {
  const [createTime, setCreateTime] = useState(60);
  const [visible, setVisble] = useState(true);
  const fetchCode = () => {
    if (!smsRule() && visible) {
      toast("请输入完整的手机号码 ");
    } else {
      fakeSendSmsCode({
        mobile: mobile,
        smsType: 'organizationAdminLogin'
      }).then(() => {
        let count = 0;
        setVisble(() => {
          let time = setInterval(() => {
            if (createTime <= 0 || count >= 60) {
              clearInterval(time);
              setVisble(() => {
                setCreateTime(60);
                return true;
              });
            } else {
              count += 1;
              setCreateTime(createTime - count);
            }
          }, 1000);
          return false;
        });
      })
    }
  };
  const smsRule = () => {
    if (mobile.length === 11) {
      return true;
    } else {
      return false;
    }
  };
  const memo = useMemo(() => {
    return (
      <View
        onClick={() => fetchCode()}
        className={`sms_code_btn ${!smsRule() && "sms_code_false"}`}
      >
        {visible ? "获取验证码" : ` ${createTime}秒后重试`}
      </View>
    );
  }, [mobile, createTime, visible]);
  return memo;
};
export default smsBtn

